import create from '../../utils/create2.js'
import store from '../../store/index.js'
import PagePromoterUtil, { PagePromoter } from '../../data/common/pagePromoter.js';
import AppSceneUtil from '../../data/common/appScene.js';
import SessionUtil from "../../data/session/session"
const Common = require('../../utils/common.js');
create(store, {

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    selectIndex: 0,
    deviceInfo: {},
    item: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 开启转发按钮
    PagePromoterUtil.showShareMenu(true).then()
    SessionUtil.getInstance().then(() => {
      // 注册当前页面的场景信息
      AppSceneUtil.setPageScene(this)
      const { type } = options
      this.queryList(type)
    })
  },
  onShow: function(){
    getApp().getSaleMode()
    SessionUtil.getInstance().then(()=>{
      // 注册当前页面的场景信息
      AppSceneUtil.setPageScene(this, false)
    })
  },
  /**
  * 查询订单
  */
  queryList(type) {
    type = type * 1 === 1 ? 1 : 3
    Common.my_request({
      url: 'AppMaster/BookDinner/getBookDinnerOrderList',
      data: {
        order_type: type
      },
      success: (res) => {
        if (res.data.result == 0) {
          const { data } = res.data
          this.setData({
            list: data
          })
        }
      }
    });
  },
  queryOrder(e) {
    const { type } = e.currentTarget.dataset
    this.setData({
      selectIndex: type * 1,
      list: []
    })
    this.queryList(type)
  },
  doDetail(e) {
    const { index } = e.currentTarget.dataset
    const { list, selectIndex } = this.data
    const { book_id } = list[index]
    if (selectIndex * 1 === 0) {
      wx.navigateTo({
        url: '/pages/index/foodsuccess/index?detail=1&order=' + book_id,
      })
    } else {
      wx.navigateTo({
        url: '/pages/index/foodsuccess/index?from=PJH&detail=1&bookid=' + book_id,
      })
    }
  },
  /**
   * 分享
   */
  onShareAppMessage: function (e) {
    const { index } = e.target.dataset
    const { list } = this.data
    const item = list[index]
    const {judge_info} = item
    // 构造并返回自定义数据
    return (new PagePromoter( '/pages/tastingShare/index', { id : item.book_id, sale: judge_info.salesman_id}))
    .setShareTitle(`您的好友${item.name}已在${item.dr_name}订好餐,希望您准时赴宴`)
    .setShareImage(judge_info.show_page_img)
    .buildShareInfo()
  }
})